import axios from "axios"
import Nproress from "nprogress"
import "nprogress/nprogress.css"

import { getUserTempId } from "../utils/setUuid"

const ajax = axios.create({
  baseURL: "http://39.98.123.211/",
  timeout: 5000,
})

ajax.interceptors.request.use((config) => {
  Nproress.start()
  const token = localStorage.getItem("token")
  console.log(token)
  if (token) config.headers.token = token
  config.headers.userTempId = getUserTempId()
  return config
})

ajax.interceptors.response.use(
  (response) => {
    Nproress.done()
    return response.data
  },
  (error) => {
    Nproress.done()
    return Promise.reject(new Error(error.message))
  }
)

export default ajax
